﻿using Microsoft.Win32;
using System;

namespace HualongExcel.Utilities
{
    public static class SaveHelper
    {

        private static readonly string subpath = @"SOFTWARE\HualongExcel\Prefs\";



        public static object GetData(string keyName, object defaultValue = default)
        {
            try
            {
                RegistryKey regkey = Registry.CurrentUser;//找到HKEY_CURRENT_USER
                                                          //在HKEY_CURRENT_USER下，找SOFTWARE\HualongExcel\Prefs"

                RegistryKey MyRegistry = regkey.OpenSubKey(subpath + keyName);
                if (MyRegistry == null)
                {
                    return defaultValue;
                }
                //在HKEY_CURRENT_USER\SOFTWARE\HualongExcel\Prefs\{keyName}"下读取keyName信息。
                return MyRegistry.GetValue(keyName);

            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        public static bool SetData(string keyName, object value)
        {
            try
            {
                //引用HKEY_CURRENT_USER
                RegistryKey key = Registry.CurrentUser;
                //在HKEY_CURRENT_USER里面的SOFTWARE创建HualongExcel\Prefs\{keyName}，在这个文件夹下创建一系列的文件
                RegistryKey MyRegistry = key.CreateSubKey(subpath + keyName);
                MyRegistry.SetValue(keyName, value);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}
